/// <reference path="../../Resources/js/jquery/jquery.debug.js" />
/// <reference name="MicrosoftAjax.debug.js" />

Type.registerNamespace("BM");

// ValueHandler object

BM.ValueHandler = function(leafItem) {
    this.leafItem = leafItem;
    this.leafItem.addOnValidationSuccess(Function.createDelegate(this, this.raiseValid));
    this.leafItem.addOnValidationError(Function.createDelegate(this, this.raiseError));    
    $(this.getInputs()).bind("change", null, Function.createDelegate(this, this.onchange))
    $(this.getInputs()).bind("focus", null, Function.createDelegate(this, this.onfocus))
    $(this.getInputs()).bind("blur", null, Function.createDelegate(this, this.onblur))
}

BM.ValueHandler.Input = "In";

BM.ValueHandler.prototype = {

    load: function() {
        /// <summary>
        /// this method is fired during value handler load
        /// </summary>
    },

    unload: function() {
        /// <summary>
        /// this method is fired during value handler unload
        /// </summary>
    },

    onfocus: function(handler) {
        /// <summary>
        /// this method is fired on any of the value handler inputs focus
        /// </summary>
    },

    onblur: function(handler) {
        /// <summary>
        /// this method is fired on any of the value handler inputs blur
        /// </summary>        
    },

    onchange: function(handler) {
        /// <summary>
        /// this method is fired on any of the value handler inputs change
        /// </summary>
        this.leafItem.raiseChanged([handler]);        
    },

    getValue: function() {
        /// <summary>
        /// This method returns the value handler's value, based on the view/edit mode.
        /// This method should not be overriden. If necessary, getViewModeValue and getEditModeValue should be overriden.
        /// </summary>
        /// <returns type="string" >
        /// The value of the value handler.
        /// </returns>
        if (this.leafItem.getMode() == MItem.ViewMode) {
            return this.getViewModeValue();
        }
        return this.getEditModeValue();
    },

    getViewModeValue: function() {
        /// <summary>
        /// This method returns the value of the value handler in case the value handler's leaf item is in view mode.
        /// If not overriden, this means that instead of the standard field, a span with the value is displayed.
        /// </summary>
        return $(this.getLeafNode()).find("span." + BM.ValueHandler.Input)[0].getAttribute('value');
    },

    getEditModeValue: function() {
        /// <summary>
        /// This method returns the value of the value handler in case the value handler's leaf item is in edit mode.
        /// If not overriden, this returns the value of the standard field.
        /// </summary>
        return this.getControlValue();
    },

    setValue: function(value) {
        /// <summary>
        /// This method checks if the value handler's leaf item allows value changing and if so, invokes a value changing method.
        /// This method should not be overriden. If necessary, setEditModeValue should be overriden.
        /// </summary>
        if (this.leafItem.getMode() == MItem.ViewMode) {
            return;
        }

        this.setEditModeValue(value);
    },

    setEditModeValue: function(value) {
        /// <summary>
        /// This method sets the value of the control and then fires the 'changed' event on the leaf item.
        /// </summary>
        this.setControlValue(value);
        this.leafItem.raiseChanged();
    },

    getControlValue: function() {
        /// <summary>
        /// This method gets the value of the control.
        /// It should be overriden in derived classes.
        /// </summary>
        var input = this.getInput();
        if (input == null) return null;
        return input.value;
    },

    setControlValue: function(value) {
        /// <summary>
        /// This method sets the value of the control.
        /// It should be overriden in derived classes.
        /// </summary>
        var input = this.getInput();
        if (input == null) return;
        input.value = value;
    },

    getLeafItem: function() {
        /// <returns type="LeafItem" >
        /// The leaf item of the value handler.
        /// </returns>
        return this.leafItem;
    },

    getLeafNode: function() {
        /// <returns domElement="true" mayBeNull="false" >
        /// The node of the leaf item.
        /// </returns>
        return this.getLeafItem().node;
    },

    getInput: function() {
        /// <returns domElement="true" mayBeNull="true" >
        /// the first of the inputs
        /// </returns>
        if (this.getInputs().length == 0) {
            return null;
        }
        return this.getInputs()[0];
    },

    getInputs: function() {
        /// <summary>
        /// this method returns all HTML data input fields within the value handler
        /// </summary>
        /// <returns type="jQuery" >
        /// by default, all DOM Elements with the 'In' class (denoting the input element)
        /// </returns>
        if (this.inputs == null) {
            this.inputs = $(this.getLeafNode()).find("." + BM.ValueHandler.Input);
        }
        return this.inputs;
    },

    raiseError: function(source, args) {
        /// <summary>
        /// This method is invoked when leaf item validation fails.        
        /// </summary>        
        $(this.getLeafNode()).addClass('ValidationError');
    },

    raiseValid: function(source, args) {
        /// <summary>
        /// This method is invoked when leaf item validation succeeds.        
        /// </summary>        
        $(this.getLeafNode()).removeClass('ValidationError');
    }

}

BM.ValueHandler.registerClass("BM.ValueHandler");